<?php 
include("include/dbcommon.php");

@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");

add_nocache_headers();
include("include/proforma_invoices_variables.php");
include('include/xtempl.php');
include('classes/runnerpage.php');

//	check if logged in
if(@$_SESSION["UserID"] && IsAdmin() && !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Add"))
{
	echo "<p>".mlang_message("NO_PERMISSIONS")."<br>Proceed to <a href=\"admin.php\">Admin Area</a> to set up user permissions</p>";
	return;
}
if(!@$_SESSION["UserID"] || !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Add"))
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	
	header("Location: login.php?message=expired"); 
	return;
}

$filename = "";
$status = "";
$message = "";
$mesClass = "";
$usermessage = "";
$error_happened = false;
$readavalues = false;

$keys = array();
$showValues = array();
$showRawValues = array();
$showFields = array();
$showDetailKeys = array();
$IsSaved = false;
$HaveData = true;
$popUpSave = false;

$sessionPrefix = $strTableName;

$onFly = false;
if(postvalue("onFly"))
	$onFly = true;

if(@$_REQUEST["editType"]=="inline")
	$inlineadd = ADD_INLINE;
elseif(@$_REQUEST["editType"]==ADD_POPUP)
{
	$inlineadd = ADD_POPUP;
	if(@$_POST["a"]=="added" && postvalue("field")=="" && postvalue("category")=="")
		$popUpSave = true;	
}
elseif(@$_REQUEST["editType"]=="addmaster")
	$inlineadd = ADD_MASTER;
elseif($onFly)
{
	$inlineadd = ADD_ONTHEFLY;
	$sessionPrefix = $strTableName."_add";
}
else
	$inlineadd = ADD_SIMPLE;

if($inlineadd == ADD_INLINE)
	$templatefile = "proforma_invoices_inline_add.htm";
else
	$templatefile = "proforma_invoices_add.htm";

$id = postvalue("id");	
if(intval($id)==0)
	$id = 1;

//If undefined session value for mastet table, but exist post value master table, than take second
//It may be happen only when use dpInline mode on page add
if(!@$_SESSION[$sessionPrefix."_mastertable"] && postvalue("mastertable"))
	$_SESSION[$sessionPrefix."_mastertable"] = postvalue("mastertable");

$xt = new Xtempl();
	
// assign an id		
$xt->assign("id",$id);
	
$auditObj = GetAuditObject($strTableName);

//array of params for classes
$params = array("pageType" => PAGE_ADD,"id" => $id,"mode" => $inlineadd);

////////////////////// data picker
$params["calendar"] = true;

////////////////////// time picker

$params['tName'] = $strTableName;
$params['strOriginalTableName'] = $strOriginalTableName;
$params['menuTablesArr'] = $menuTablesArr;
$params['xt'] = &$xt;
$params['needSearchClauseObj'] = false;
$params['includes_js']=$includes_js;
$params['includes_jsreq']=$includes_jsreq;
$params['includes_css']=$includes_css;
$params['locale_info']=$locale_info;
$params['pageAddLikeInline'] = ($inlineadd==ADD_INLINE);
$params['useTabsOnAdd'] = useTabsOnAdd($strTableName);
if($params['useTabsOnAdd'])
	$params['arrAddTabs'] = GetAddTabs($strTableName);
$pageObject = new RunnerPage($params);

//Get detail table keys	
$detailKeys = $pageObject->detailKeysByM;

//Array of fields, which appear on add page
$addFields = $pageObject->getFieldsByPageType();

// add onload event
//$onLoadJsCode = GetTableData($pageObject->tName, ".jsOnloadAdd", '');
//$pageObject->addOnLoadJsEvent($onLoadJsCode);

if ($inlineadd==ADD_SIMPLE)
{
	// add button events if exist
	$pageObject->addButtonHandlers();
}

$url_page=substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1,12);

//For show detail tables on master page add
if($inlineadd==ADD_SIMPLE || $inlineadd==ADD_MASTER || $inlineadd==ADD_POPUP)
{
	$dpParams = array();
	if($pageObject->isShowDetailTables)
	{
		$ids = $id;
			$mKeys["proforma_invoice_items"] = GetMasterKeysByDetailTable("proforma_invoice_items", $strTableName);
		$dpParams['strTableNames'][] = "proforma_invoice_items";
		$dpParams['ids'][] = ++$ids;
		$pageObject->jsSettings['tableSettings'][$strTableName]['dpParams'] = array('tableNames'=>$dpParams['strTableNames'], 'ids'=>$dpParams['ids']);
		if($inlineadd==ADD_SIMPLE)
			$pageObject->AddJSFile("include/detailspreview");	
	}
}

//	Before Process event
if($eventObj->exists("BeforeProcessAdd"))
	$eventObj->BeforeProcessAdd($conn);

// proccess captcha
if ($inlineadd==ADD_SIMPLE || $inlineadd==ADD_MASTER || $inlineadd==ADD_POPUP)
	if($pageObject->captchaExists())
		$pageObject->doCaptchaCode();	
	
// insert new record if we have to
if(@$_POST["a"]=="added")
{
	$afilename_values=array();
	$avalues=array();
	$blobfields=array();
//	processing account_id - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_account_id_".$id);
		$type=postvalue("type_account_id_".$id);
		if (FieldSubmitted("account_id_".$id))
		{
				$value=prepare_for_db("account_id",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "account_id"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["account_id"]=$value;
		}
		}
//	processibng account_id - end
//	processing account_address - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_account_address_".$id);
		$type=postvalue("type_account_address_".$id);
		if (FieldSubmitted("account_address_".$id))
		{
				$value=prepare_for_db("account_address",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "account_address"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["account_address"]=$value;
		}
		}
//	processibng account_address - end
//	processing consignee_id - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_consignee_id_".$id);
		$type=postvalue("type_consignee_id_".$id);
		if (FieldSubmitted("consignee_id_".$id))
		{
				$value=prepare_for_db("consignee_id",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "consignee_id"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["consignee_id"]=$value;
		}
		}
//	processibng consignee_id - end
//	processing consignee_address - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_consignee_address_".$id);
		$type=postvalue("type_consignee_address_".$id);
		if (FieldSubmitted("consignee_address_".$id))
		{
				$value=prepare_for_db("consignee_address",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "consignee_address"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["consignee_address"]=$value;
		}
		}
//	processibng consignee_address - end
//	processing notify_id - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_notify_id_".$id);
		$type=postvalue("type_notify_id_".$id);
		if (FieldSubmitted("notify_id_".$id))
		{
				$value=prepare_for_db("notify_id",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "notify_id"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["notify_id"]=$value;
		}
		}
//	processibng notify_id - end
//	processing notify_address - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_notify_address_".$id);
		$type=postvalue("type_notify_address_".$id);
		if (FieldSubmitted("notify_address_".$id))
		{
				$value=prepare_for_db("notify_address",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "notify_address"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["notify_address"]=$value;
		}
		}
//	processibng notify_address - end
//	processing buyer_id - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_buyer_id_".$id);
		$type=postvalue("type_buyer_id_".$id);
		if (FieldSubmitted("buyer_id_".$id))
		{
				$value=prepare_for_db("buyer_id",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "buyer_id"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["buyer_id"]=$value;
		}
		}
//	processibng buyer_id - end
//	processing buyer_address - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_buyer_address_".$id);
		$type=postvalue("type_buyer_address_".$id);
		if (FieldSubmitted("buyer_address_".$id))
		{
				$value=prepare_for_db("buyer_address",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "buyer_address"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["buyer_address"]=$value;
		}
		}
//	processibng buyer_address - end
//	processing payment_term_id - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_payment_term_id_".$id);
		$type=postvalue("type_payment_term_id_".$id);
		if (FieldSubmitted("payment_term_id_".$id))
		{
				$value=prepare_for_db("payment_term_id",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "payment_term_id"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["payment_term_id"]=$value;
		}
		}
//	processibng payment_term_id - end
//	processing payment_term_other - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_payment_term_other_".$id);
		$type=postvalue("type_payment_term_other_".$id);
		if (FieldSubmitted("payment_term_other_".$id))
		{
				$value=prepare_for_db("payment_term_other",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "payment_term_other"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["payment_term_other"]=$value;
		}
		}
//	processibng payment_term_other - end
//	processing currency_id - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_currency_id_".$id);
		$type=postvalue("type_currency_id_".$id);
		if (FieldSubmitted("currency_id_".$id))
		{
				$value=prepare_for_db("currency_id",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "currency_id"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["currency_id"]=$value;
		}
		}
//	processibng currency_id - end
//	processing bank_name_id - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_bank_name_id_".$id);
		$type=postvalue("type_bank_name_id_".$id);
		if (FieldSubmitted("bank_name_id_".$id))
		{
				$value=prepare_for_db("bank_name_id",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "bank_name_id"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["bank_name_id"]=$value;
		}
		}
//	processibng bank_name_id - end
//	processing bank_term1 - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_bank_term1_".$id);
		$type=postvalue("type_bank_term1_".$id);
		if (FieldSubmitted("bank_term1_".$id))
		{
				$value=prepare_for_db("bank_term1",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "bank_term1"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["bank_term1"]=$value;
		}
		}
//	processibng bank_term1 - end
//	processing bank_term2 - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_bank_term2_".$id);
		$type=postvalue("type_bank_term2_".$id);
		if (FieldSubmitted("bank_term2_".$id))
		{
				$value=prepare_for_db("bank_term2",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "bank_term2"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["bank_term2"]=$value;
		}
		}
//	processibng bank_term2 - end
//	processing additional_charges_frieght_type - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_additional_charges_frieght_type_".$id);
		$type=postvalue("type_additional_charges_frieght_type_".$id);
		if (FieldSubmitted("additional_charges_frieght_type_".$id))
		{
				$value=prepare_for_db("additional_charges_frieght_type",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "additional_charges_frieght_type"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["additional_charges_frieght_type"]=$value;
		}
		}
//	processibng additional_charges_frieght_type - end
//	processing additional_charges_legalization - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_additional_charges_legalization_".$id);
		$type=postvalue("type_additional_charges_legalization_".$id);
		if (FieldSubmitted("additional_charges_legalization_".$id))
		{
				$value=prepare_for_db("additional_charges_legalization",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "additional_charges_legalization"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["additional_charges_legalization"]=$value;
		}
		}
//	processibng additional_charges_legalization - end
//	processing additional_charges_inspection - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_additional_charges_inspection_".$id);
		$type=postvalue("type_additional_charges_inspection_".$id);
		if (FieldSubmitted("additional_charges_inspection_".$id))
		{
				$value=prepare_for_db("additional_charges_inspection",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "additional_charges_inspection"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["additional_charges_inspection"]=$value;
		}
		}
//	processibng additional_charges_inspection - end
//	processing additional_charges_special_packing - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_additional_charges_special_packing_".$id);
		$type=postvalue("type_additional_charges_special_packing_".$id);
		if (FieldSubmitted("additional_charges_special_packing_".$id))
		{
				$value=prepare_for_db("additional_charges_special_packing",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "additional_charges_special_packing"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["additional_charges_special_packing"]=$value;
		}
		}
//	processibng additional_charges_special_packing - end
//	processing others1_key - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_others1_key_".$id);
		$type=postvalue("type_others1_key_".$id);
		if (FieldSubmitted("others1_key_".$id))
		{
				$value=prepare_for_db("others1_key",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "others1_key"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["others1_key"]=$value;
		}
		}
//	processibng others1_key - end
//	processing others1_value - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_others1_value_".$id);
		$type=postvalue("type_others1_value_".$id);
		if (FieldSubmitted("others1_value_".$id))
		{
				$value=prepare_for_db("others1_value",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "others1_value"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["others1_value"]=$value;
		}
		}
//	processibng others1_value - end
//	processing others2_key - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_others2_key_".$id);
		$type=postvalue("type_others2_key_".$id);
		if (FieldSubmitted("others2_key_".$id))
		{
				$value=prepare_for_db("others2_key",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "others2_key"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["others2_key"]=$value;
		}
		}
//	processibng others2_key - end
//	processing others2_value - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_others2_value_".$id);
		$type=postvalue("type_others2_value_".$id);
		if (FieldSubmitted("others2_value_".$id))
		{
				$value=prepare_for_db("others2_value",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "others2_value"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["others2_value"]=$value;
		}
		}
//	processibng others2_value - end
//	processing others3_key - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_others3_key_".$id);
		$type=postvalue("type_others3_key_".$id);
		if (FieldSubmitted("others3_key_".$id))
		{
				$value=prepare_for_db("others3_key",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "others3_key"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["others3_key"]=$value;
		}
		}
//	processibng others3_key - end
//	processing others3_value - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_others3_value_".$id);
		$type=postvalue("type_others3_value_".$id);
		if (FieldSubmitted("others3_value_".$id))
		{
				$value=prepare_for_db("others3_value",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "others3_value"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["others3_value"]=$value;
		}
		}
//	processibng others3_value - end
//	processing terms_and_conditions - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_terms_and_conditions_".$id);
		$type=postvalue("type_terms_and_conditions_".$id);
		if (FieldSubmitted("terms_and_conditions_".$id))
		{
				$value=prepare_for_db("terms_and_conditions",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "terms_and_conditions"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["terms_and_conditions"]=$value;
		}
		}
//	processibng terms_and_conditions - end
//	processing term_of_sale1 - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_term_of_sale1_".$id);
		$type=postvalue("type_term_of_sale1_".$id);
		if (FieldSubmitted("term_of_sale1_".$id))
		{
				$value=prepare_for_db("term_of_sale1",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "term_of_sale1"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["term_of_sale1"]=$value;
		}
		}
//	processibng term_of_sale1 - end
//	processing term_of_sale2 - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_term_of_sale2_".$id);
		$type=postvalue("type_term_of_sale2_".$id);
		if (FieldSubmitted("term_of_sale2_".$id))
		{
				$value=prepare_for_db("term_of_sale2",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "term_of_sale2"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["term_of_sale2"]=$value;
		}
		}
//	processibng term_of_sale2 - end
//	processing proforma_invoice_no - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_proforma_invoice_no_".$id);
		$type=postvalue("type_proforma_invoice_no_".$id);
		if (FieldSubmitted("proforma_invoice_no_".$id))
		{
				$value=prepare_for_db("proforma_invoice_no",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "proforma_invoice_no"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["proforma_invoice_no"]=$value;
		}
		}
//	processibng proforma_invoice_no - end
//	processing proforma_invoice_date - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_proforma_invoice_date_".$id);
		$type=postvalue("type_proforma_invoice_date_".$id);
		if (FieldSubmitted("proforma_invoice_date_".$id))
		{
				$value=prepare_for_db("proforma_invoice_date",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "proforma_invoice_date"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["proforma_invoice_date"]=$value;
		}
		}
//	processibng proforma_invoice_date - end
//	processing customer_reference - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_customer_reference_".$id);
		$type=postvalue("type_customer_reference_".$id);
		if (FieldSubmitted("customer_reference_".$id))
		{
				$value=prepare_for_db("customer_reference",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "customer_reference"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["customer_reference"]=$value;
		}
		}
//	processibng customer_reference - end
//	processing commercial_invoice_no - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_commercial_invoice_no_".$id);
		$type=postvalue("type_commercial_invoice_no_".$id);
		if (FieldSubmitted("commercial_invoice_no_".$id))
		{
				$value=prepare_for_db("commercial_invoice_no",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "commercial_invoice_no"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["commercial_invoice_no"]=$value;
		}
		}
//	processibng commercial_invoice_no - end
//	processing commercial_invoice_date - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_commercial_invoice_date_".$id);
		$type=postvalue("type_commercial_invoice_date_".$id);
		if (FieldSubmitted("commercial_invoice_date_".$id))
		{
				$value=prepare_for_db("commercial_invoice_date",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "commercial_invoice_date"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["commercial_invoice_date"]=$value;
		}
		}
//	processibng commercial_invoice_date - end
//	processing customer_reference_date - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_customer_reference_date_".$id);
		$type=postvalue("type_customer_reference_date_".$id);
		if (FieldSubmitted("customer_reference_date_".$id))
		{
				$value=prepare_for_db("customer_reference_date",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "customer_reference_date"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["customer_reference_date"]=$value;
		}
		}
//	processibng customer_reference_date - end
//	processing shipment_mode_id - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_shipment_mode_id_".$id);
		$type=postvalue("type_shipment_mode_id_".$id);
		if (FieldSubmitted("shipment_mode_id_".$id))
		{
				$value=prepare_for_db("shipment_mode_id",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "shipment_mode_id"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["shipment_mode_id"]=$value;
		}
		}
//	processibng shipment_mode_id - end
//	processing estimated_shipment_date - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_estimated_shipment_date_".$id);
		$type=postvalue("type_estimated_shipment_date_".$id);
		if (FieldSubmitted("estimated_shipment_date_".$id))
		{
				$value=prepare_for_db("estimated_shipment_date",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "estimated_shipment_date"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["estimated_shipment_date"]=$value;
		}
		}
//	processibng estimated_shipment_date - end
//	processing estimated_shipment_date_custom - start
	$inlineAddOption = true;
	if($inlineAddOption)
	{
		$value = postvalue("value_estimated_shipment_date_custom_".$id);
		$type=postvalue("type_estimated_shipment_date_custom_".$id);
		if (FieldSubmitted("estimated_shipment_date_custom_".$id))
		{
				$value=prepare_for_db("estimated_shipment_date_custom",$value,$type);
		}
		else
			$value=false;
		
		if(!($value===false))
		{
	
	
						if(1 && "estimated_shipment_date_custom"=="password" && $url_page=="admin_users_")
				$value=md5($value);
			$avalues["estimated_shipment_date_custom"]=$value;
		}
		}
//	processibng estimated_shipment_date_custom - end




	$failed_inline_add=false;
//	add filenames to values
	foreach($afilename_values as $akey=>$value)
		$avalues[$akey]=$value;
	
//	before Add event
	$retval = true;
	if($eventObj->exists("BeforeAdd"))
		$retval=$eventObj->BeforeAdd($avalues,$usermessage,(bool)$inlineadd);
	if($retval && $pageObject->isCaptchaOk)
	{
		$_SESSION[$strTableName."_count_captcha"] = $_SESSION[$strTableName."_count_captcha"]+1;
		if(DoInsertRecord($strOriginalTableName,$avalues,$blobfields,$id,$pageObject))
		{
			$IsSaved=true;
//	after edit event
			if($auditObj || $eventObj->exists("AfterAdd"))
			{
				foreach($keys as $idx=>$val)
					$avalues[$idx]=$val;
			}
			
			if($auditObj)
				$auditObj->LogAdd($strTableName,$avalues,$keys);

			if($eventObj->exists("AfterAdd"))
				$eventObj->AfterAdd($avalues,$keys,(bool)$inlineadd);
				
			if($inlineadd==ADD_SIMPLE || $inlineadd==ADD_MASTER)
			{
				$permis = array();
				$keylink = "";$k = 0;
				foreach($keys as $idx=>$val)
				{
					if($k!=0)
						$keylink .="&";
					$keylink .="editid".(++$k)."=".htmlspecialchars(rawurlencode(@$val));
				}
				$permis = $pageObject->getPermissions();				
				if (count($keys))
				{
					$message .="</br>";
					if(GetTableData($strTableName,".edit",false) && $permis['edit'])
						$message .='&nbsp;<a href=\'proforma_invoices_edit.php?'.$keylink.'\'>'.mlang_message("EDIT").'</a>&nbsp;';
					if(GetTableData($strTableName,".view",false) && $permis['search'])
						$message .='&nbsp;<a href=\'proforma_invoices_view.php?'.$keylink.'\'>'.mlang_message("VIEW").'</a>&nbsp;';
				}
				$mesClass = "mes_ok";	
			}
		}
		elseif($inlineadd!=ADD_INLINE)
			$mesClass = "mes_not";	
	}
	else
	{
		$message = $usermessage;
		$status="DECLINED";
		$readavalues=true;
	}
}

$message = "<div class='message ".$mesClass."'>".$message."</div>";

// PRG rule, to avoid POSTDATA resend
if (no_output_done() && $inlineadd==ADD_SIMPLE && $IsSaved)
{
	// saving message
	$_SESSION["message"] = ($message ? $message : "");
	// redirect
	header("Location: proforma_invoices_".$pageObject->getPageType().".php");
	// turned on output buffering, so we need to stop script
	exit();
}

if($inlineadd==ADD_MASTER && $IsSaved)
	$_SESSION["message"] = ($message ? $message : "");
	
// for PRG rule, to avoid POSTDATA resend. Saving mess in session
if($inlineadd==ADD_SIMPLE && isset($_SESSION["message"]))
{
	$message = $_SESSION["message"];
	unset($_SESSION["message"]);
}

$defvalues=array();

//	copy record
if(array_key_exists("copyid1",$_REQUEST) || array_key_exists("editid1",$_REQUEST))
{
	$copykeys=array();
	if(array_key_exists("copyid1",$_REQUEST))
	{
		$copykeys["id"]=postvalue("copyid1");
	}
	else
	{
		$copykeys["id"]=postvalue("editid1");
	}
	$strWhere=KeyWhere($copykeys);
	$strSQL = gSQLWhere($strWhere);

	LogInfo($strSQL);
	$rs=db_query($strSQL,$conn);
	$defvalues=db_fetch_array($rs);
	if(!$defvalues)
		$defvalues=array();
//	clear key fields
	$defvalues["id"]="";
//call CopyOnLoad event
	if($eventObj->exists("CopyOnLoad"))
		$eventObj->CopyOnLoad($defvalues,$strWhere);
}
else
{
	$defvalues["proforma_invoice_date"]=now();
	$defvalues["estimated_shipment_date"]=now();
}


if($readavalues)
{
	$defvalues["account_id"]=@$avalues["account_id"];
	$defvalues["account_address"]=@$avalues["account_address"];
	$defvalues["buyer_id"]=@$avalues["buyer_id"];
	$defvalues["buyer_address"]=@$avalues["buyer_address"];
	$defvalues["consignee_id"]=@$avalues["consignee_id"];
	$defvalues["consignee_address"]=@$avalues["consignee_address"];
	$defvalues["notify_id"]=@$avalues["notify_id"];
	$defvalues["notify_address"]=@$avalues["notify_address"];
	$defvalues["proforma_invoice_no"]=@$avalues["proforma_invoice_no"];
	$defvalues["proforma_invoice_date"]=@$avalues["proforma_invoice_date"];
	$defvalues["customer_reference"]=@$avalues["customer_reference"];
	$defvalues["customer_reference_date"]=@$avalues["customer_reference_date"];
	$defvalues["commercial_invoice_no"]=@$avalues["commercial_invoice_no"];
	$defvalues["commercial_invoice_date"]=@$avalues["commercial_invoice_date"];
	$defvalues["term_of_sale1"]=@$avalues["term_of_sale1"];
	$defvalues["term_of_sale2"]=@$avalues["term_of_sale2"];
	$defvalues["shipment_mode_id"]=@$avalues["shipment_mode_id"];
	$defvalues["estimated_shipment_date"]=@$avalues["estimated_shipment_date"];
	$defvalues["estimated_shipment_date_custom"]=@$avalues["estimated_shipment_date_custom"];
	$defvalues["additional_charges_frieght_type"]=@$avalues["additional_charges_frieght_type"];
	$defvalues["additional_charges_legalization"]=@$avalues["additional_charges_legalization"];
	$defvalues["additional_charges_inspection"]=@$avalues["additional_charges_inspection"];
	$defvalues["additional_charges_special_packing"]=@$avalues["additional_charges_special_packing"];
	$defvalues["others1_key"]=@$avalues["others1_key"];
	$defvalues["others2_key"]=@$avalues["others2_key"];
	$defvalues["others3_key"]=@$avalues["others3_key"];
	$defvalues["others1_value"]=@$avalues["others1_value"];
	$defvalues["others2_value"]=@$avalues["others2_value"];
	$defvalues["others3_value"]=@$avalues["others3_value"];
	$defvalues["payment_term_id"]=@$avalues["payment_term_id"];
	$defvalues["payment_term_other"]=@$avalues["payment_term_other"];
	$defvalues["bank_name_id"]=@$avalues["bank_name_id"];
	$defvalues["bank_term1"]=@$avalues["bank_term1"];
	$defvalues["bank_term2"]=@$avalues["bank_term2"];
	$defvalues["currency_id"]=@$avalues["currency_id"];
	$defvalues["terms_and_conditions"]=@$avalues["terms_and_conditions"];
}

if($eventObj->exists("ProcessValuesAdd"))
	$eventObj->ProcessValuesAdd($defvalues);


//for basic files
$includes="";

if($inlineadd!=ADD_INLINE)
{
	if($inlineadd!=ADD_ONTHEFLY && $inlineadd!=ADD_POPUP)
	{
		$includes .="<script language=\"JavaScript\" src=\"include/jquery.js\"></script>\r\n";
		if ($pageObject->debugJSMode===true)
		{
			/*$includes.="<script type=\"text/javascript\" src=\"include/runnerJS/Runner.js\"></script>\r\n".
				"<script type=\"text/javascript\" src=\"include/runnerJS/Util.js\"></script>\r\n".
				"<script type=\"text/javascript\" src=\"include/runnerJS/Observer.js\"></script>\r\n";*/
			$includes.="<script language=\"JavaScript\" src=\"include/runnerJS/RunnerBase.js\"></script>\r\n";
		}
		else 
			$includes .= "<script type=\"text/javascript\" src=\"include/runnerJS/RunnerBase.js\"></script>";
		
		$includes.="<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n";
		$includes.="<div id=\"search_suggest\"></div>\r\n";
	}
	
	if(!$pageObject->isAppearOnTabs("account_id"))
		$xt->assign("account_id_fieldblock",true);
	else
		$xt->assign("account_id_tabfieldblock",true);
	$xt->assign("account_id_label",true);
	if(isEnableSection508())
		$xt->assign_section("account_id_label","<label for=\"".GetInputElementId("account_id", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("account_address"))
		$xt->assign("account_address_fieldblock",true);
	else
		$xt->assign("account_address_tabfieldblock",true);
	$xt->assign("account_address_label",true);
	if(isEnableSection508())
		$xt->assign_section("account_address_label","<label for=\"".GetInputElementId("account_address", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("buyer_id"))
		$xt->assign("buyer_id_fieldblock",true);
	else
		$xt->assign("buyer_id_tabfieldblock",true);
	$xt->assign("buyer_id_label",true);
	if(isEnableSection508())
		$xt->assign_section("buyer_id_label","<label for=\"".GetInputElementId("buyer_id", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("buyer_address"))
		$xt->assign("buyer_address_fieldblock",true);
	else
		$xt->assign("buyer_address_tabfieldblock",true);
	$xt->assign("buyer_address_label",true);
	if(isEnableSection508())
		$xt->assign_section("buyer_address_label","<label for=\"".GetInputElementId("buyer_address", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("consignee_id"))
		$xt->assign("consignee_id_fieldblock",true);
	else
		$xt->assign("consignee_id_tabfieldblock",true);
	$xt->assign("consignee_id_label",true);
	if(isEnableSection508())
		$xt->assign_section("consignee_id_label","<label for=\"".GetInputElementId("consignee_id", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("consignee_address"))
		$xt->assign("consignee_address_fieldblock",true);
	else
		$xt->assign("consignee_address_tabfieldblock",true);
	$xt->assign("consignee_address_label",true);
	if(isEnableSection508())
		$xt->assign_section("consignee_address_label","<label for=\"".GetInputElementId("consignee_address", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("notify_id"))
		$xt->assign("notify_id_fieldblock",true);
	else
		$xt->assign("notify_id_tabfieldblock",true);
	$xt->assign("notify_id_label",true);
	if(isEnableSection508())
		$xt->assign_section("notify_id_label","<label for=\"".GetInputElementId("notify_id", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("notify_address"))
		$xt->assign("notify_address_fieldblock",true);
	else
		$xt->assign("notify_address_tabfieldblock",true);
	$xt->assign("notify_address_label",true);
	if(isEnableSection508())
		$xt->assign_section("notify_address_label","<label for=\"".GetInputElementId("notify_address", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("proforma_invoice_no"))
		$xt->assign("proforma_invoice_no_fieldblock",true);
	else
		$xt->assign("proforma_invoice_no_tabfieldblock",true);
	$xt->assign("proforma_invoice_no_label",true);
	if(isEnableSection508())
		$xt->assign_section("proforma_invoice_no_label","<label for=\"".GetInputElementId("proforma_invoice_no", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("proforma_invoice_date"))
		$xt->assign("proforma_invoice_date_fieldblock",true);
	else
		$xt->assign("proforma_invoice_date_tabfieldblock",true);
	$xt->assign("proforma_invoice_date_label",true);
	if(isEnableSection508())
		$xt->assign_section("proforma_invoice_date_label","<label for=\"".GetInputElementId("proforma_invoice_date", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("customer_reference"))
		$xt->assign("customer_reference_fieldblock",true);
	else
		$xt->assign("customer_reference_tabfieldblock",true);
	$xt->assign("customer_reference_label",true);
	if(isEnableSection508())
		$xt->assign_section("customer_reference_label","<label for=\"".GetInputElementId("customer_reference", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("customer_reference_date"))
		$xt->assign("customer_reference_date_fieldblock",true);
	else
		$xt->assign("customer_reference_date_tabfieldblock",true);
	$xt->assign("customer_reference_date_label",true);
	if(isEnableSection508())
		$xt->assign_section("customer_reference_date_label","<label for=\"".GetInputElementId("customer_reference_date", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("commercial_invoice_no"))
		$xt->assign("commercial_invoice_no_fieldblock",true);
	else
		$xt->assign("commercial_invoice_no_tabfieldblock",true);
	$xt->assign("commercial_invoice_no_label",true);
	if(isEnableSection508())
		$xt->assign_section("commercial_invoice_no_label","<label for=\"".GetInputElementId("commercial_invoice_no", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("commercial_invoice_date"))
		$xt->assign("commercial_invoice_date_fieldblock",true);
	else
		$xt->assign("commercial_invoice_date_tabfieldblock",true);
	$xt->assign("commercial_invoice_date_label",true);
	if(isEnableSection508())
		$xt->assign_section("commercial_invoice_date_label","<label for=\"".GetInputElementId("commercial_invoice_date", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("term_of_sale1"))
		$xt->assign("term_of_sale1_fieldblock",true);
	else
		$xt->assign("term_of_sale1_tabfieldblock",true);
	$xt->assign("term_of_sale1_label",true);
	if(isEnableSection508())
		$xt->assign_section("term_of_sale1_label","<label for=\"".GetInputElementId("term_of_sale1", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("term_of_sale2"))
		$xt->assign("term_of_sale2_fieldblock",true);
	else
		$xt->assign("term_of_sale2_tabfieldblock",true);
	$xt->assign("term_of_sale2_label",true);
	if(isEnableSection508())
		$xt->assign_section("term_of_sale2_label","<label for=\"".GetInputElementId("term_of_sale2", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("shipment_mode_id"))
		$xt->assign("shipment_mode_id_fieldblock",true);
	else
		$xt->assign("shipment_mode_id_tabfieldblock",true);
	$xt->assign("shipment_mode_id_label",true);
	if(isEnableSection508())
		$xt->assign_section("shipment_mode_id_label","<label for=\"".GetInputElementId("shipment_mode_id", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("estimated_shipment_date"))
		$xt->assign("estimated_shipment_date_fieldblock",true);
	else
		$xt->assign("estimated_shipment_date_tabfieldblock",true);
	$xt->assign("estimated_shipment_date_label",true);
	if(isEnableSection508())
		$xt->assign_section("estimated_shipment_date_label","<label for=\"".GetInputElementId("estimated_shipment_date", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("estimated_shipment_date_custom"))
		$xt->assign("estimated_shipment_date_custom_fieldblock",true);
	else
		$xt->assign("estimated_shipment_date_custom_tabfieldblock",true);
	$xt->assign("estimated_shipment_date_custom_label",true);
	if(isEnableSection508())
		$xt->assign_section("estimated_shipment_date_custom_label","<label for=\"".GetInputElementId("estimated_shipment_date_custom", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("additional_charges_frieght_type"))
		$xt->assign("additional_charges_frieght_type_fieldblock",true);
	else
		$xt->assign("additional_charges_frieght_type_tabfieldblock",true);
	$xt->assign("additional_charges_frieght_type_label",true);
	if(isEnableSection508())
		$xt->assign_section("additional_charges_frieght_type_label","<label for=\"".GetInputElementId("additional_charges_frieght_type", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("additional_charges_legalization"))
		$xt->assign("additional_charges_legalization_fieldblock",true);
	else
		$xt->assign("additional_charges_legalization_tabfieldblock",true);
	$xt->assign("additional_charges_legalization_label",true);
	if(isEnableSection508())
		$xt->assign_section("additional_charges_legalization_label","<label for=\"".GetInputElementId("additional_charges_legalization", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("additional_charges_inspection"))
		$xt->assign("additional_charges_inspection_fieldblock",true);
	else
		$xt->assign("additional_charges_inspection_tabfieldblock",true);
	$xt->assign("additional_charges_inspection_label",true);
	if(isEnableSection508())
		$xt->assign_section("additional_charges_inspection_label","<label for=\"".GetInputElementId("additional_charges_inspection", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("additional_charges_special_packing"))
		$xt->assign("additional_charges_special_packing_fieldblock",true);
	else
		$xt->assign("additional_charges_special_packing_tabfieldblock",true);
	$xt->assign("additional_charges_special_packing_label",true);
	if(isEnableSection508())
		$xt->assign_section("additional_charges_special_packing_label","<label for=\"".GetInputElementId("additional_charges_special_packing", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("others1_key"))
		$xt->assign("others1_key_fieldblock",true);
	else
		$xt->assign("others1_key_tabfieldblock",true);
	$xt->assign("others1_key_label",true);
	if(isEnableSection508())
		$xt->assign_section("others1_key_label","<label for=\"".GetInputElementId("others1_key", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("others2_key"))
		$xt->assign("others2_key_fieldblock",true);
	else
		$xt->assign("others2_key_tabfieldblock",true);
	$xt->assign("others2_key_label",true);
	if(isEnableSection508())
		$xt->assign_section("others2_key_label","<label for=\"".GetInputElementId("others2_key", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("others3_key"))
		$xt->assign("others3_key_fieldblock",true);
	else
		$xt->assign("others3_key_tabfieldblock",true);
	$xt->assign("others3_key_label",true);
	if(isEnableSection508())
		$xt->assign_section("others3_key_label","<label for=\"".GetInputElementId("others3_key", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("others1_value"))
		$xt->assign("others1_value_fieldblock",true);
	else
		$xt->assign("others1_value_tabfieldblock",true);
	$xt->assign("others1_value_label",true);
	if(isEnableSection508())
		$xt->assign_section("others1_value_label","<label for=\"".GetInputElementId("others1_value", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("others2_value"))
		$xt->assign("others2_value_fieldblock",true);
	else
		$xt->assign("others2_value_tabfieldblock",true);
	$xt->assign("others2_value_label",true);
	if(isEnableSection508())
		$xt->assign_section("others2_value_label","<label for=\"".GetInputElementId("others2_value", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("others3_value"))
		$xt->assign("others3_value_fieldblock",true);
	else
		$xt->assign("others3_value_tabfieldblock",true);
	$xt->assign("others3_value_label",true);
	if(isEnableSection508())
		$xt->assign_section("others3_value_label","<label for=\"".GetInputElementId("others3_value", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("payment_term_id"))
		$xt->assign("payment_term_id_fieldblock",true);
	else
		$xt->assign("payment_term_id_tabfieldblock",true);
	$xt->assign("payment_term_id_label",true);
	if(isEnableSection508())
		$xt->assign_section("payment_term_id_label","<label for=\"".GetInputElementId("payment_term_id", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("payment_term_other"))
		$xt->assign("payment_term_other_fieldblock",true);
	else
		$xt->assign("payment_term_other_tabfieldblock",true);
	$xt->assign("payment_term_other_label",true);
	if(isEnableSection508())
		$xt->assign_section("payment_term_other_label","<label for=\"".GetInputElementId("payment_term_other", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("bank_name_id"))
		$xt->assign("bank_name_id_fieldblock",true);
	else
		$xt->assign("bank_name_id_tabfieldblock",true);
	$xt->assign("bank_name_id_label",true);
	if(isEnableSection508())
		$xt->assign_section("bank_name_id_label","<label for=\"".GetInputElementId("bank_name_id", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("bank_term1"))
		$xt->assign("bank_term1_fieldblock",true);
	else
		$xt->assign("bank_term1_tabfieldblock",true);
	$xt->assign("bank_term1_label",true);
	if(isEnableSection508())
		$xt->assign_section("bank_term1_label","<label for=\"".GetInputElementId("bank_term1", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("bank_term2"))
		$xt->assign("bank_term2_fieldblock",true);
	else
		$xt->assign("bank_term2_tabfieldblock",true);
	$xt->assign("bank_term2_label",true);
	if(isEnableSection508())
		$xt->assign_section("bank_term2_label","<label for=\"".GetInputElementId("bank_term2", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("currency_id"))
		$xt->assign("currency_id_fieldblock",true);
	else
		$xt->assign("currency_id_tabfieldblock",true);
	$xt->assign("currency_id_label",true);
	if(isEnableSection508())
		$xt->assign_section("currency_id_label","<label for=\"".GetInputElementId("currency_id", $id)."\">","</label>");
	
	if(!$pageObject->isAppearOnTabs("terms_and_conditions"))
		$xt->assign("terms_and_conditions_fieldblock",true);
	else
		$xt->assign("terms_and_conditions_tabfieldblock",true);
	$xt->assign("terms_and_conditions_label",true);
	if(isEnableSection508())
		$xt->assign_section("terms_and_conditions_label","<label for=\"".GetInputElementId("terms_and_conditions", $id)."\">","</label>");
	
	
	
	if($inlineadd!=ADD_ONTHEFLY && $inlineadd!=ADD_POPUP)
	{
		$pageObject->body["begin"] .= $includes;
		if($pageObject->isShowDetailTables)
			$pageObject->body["begin"].= "<div id=\"master_details\" onmouseover=\"RollDetailsLink.showPopup();\" onmouseout=\"RollDetailsLink.hidePopup();\"> </div>";
		$xt->assign("backbutton_attrs","id=\"backButton".$id."\"");
		$xt->assign("back_button",true);
		//$xt->assign('addForm', true);
	}
	else
	{		
		$xt->assign("cancelbutton_attrs", "id=\"cancelButton".$id."\"");
		$xt->assign("cancel_button",true);
		$xt->assign("header","");
	}
	$xt->assign("save_button",true);
}
$xt->assign("savebutton_attrs","id=\"saveButton".$id."\"");
if($message)
{
	$xt->assign("message_block",true);
	$xt->assign("message",$message);
}
/*
if($inlineadd == ADD_ONTHEFLY || $inlineadd == ADD_POPUP)
{
	$xt->assign("message_block",true);
}
*/

$readonlyfields=array();

//	show readonly fields
$linkdata="";

if(@$_POST["a"]=="added" && $inlineadd==ADD_ONTHEFLY)
{
	if( !$error_happened && $status!="DECLINED")
	{
		$LookupSQL = "";
		$linkfield = "";
		$dispfield = "";
		if($LookupSQL)
			$LookupSQL.=" from ".AddTableWrappers($strOriginalTableName);

		$data=0;
		if(count($keys) && $LookupSQL)
		{
			$where=KeyWhere($keys);
			$LookupSQL.=" where ".$where;
			$rs=db_query($LookupSQL,$conn);
			$data=db_fetch_numarray($rs);
		}
		if($data)
		{
			$respData = array($linkfield=>@$data[0], $dispfield=>@$data[1]);
		}
		else
		{
			$respData = array($linkfield=>@$avalues[$linkfield], $dispfield=>@$avalues[$dispfield]);
		}		
		$returnJSON['success'] = true;
		$returnJSON['keys'] = $keys;
		$returnJSON['vals'] = $respData;
		$returnJSON['fields'] = $showFields;
	}
	else
	{
		$returnJSON['success'] = false;
		$returnJSON['message'] = $message;
	}
	echo "<textarea>".htmlspecialchars(my_json_encode($returnJSON))."</textarea>";
	exit();
}

if(@$_POST["a"]=="added" && ($inlineadd == ADD_INLINE || $inlineadd == ADD_MASTER || $inlineadd==ADD_POPUP)) 
{
	//Preparation   view values
	//	get current values and show edit controls
	$dispFieldAlias = "";
	$data=0;
	if(count($keys))
	{
		$where=KeyWhere($keys);
			
		$sqlHead = $gQuery->HeadToSql();
		$sqlGroupBy = $gQuery->GroupByToSql();
		$oHaving = $gQuery->Having();
		$sqlHaving = $oHaving->toSql($gQuery);
		
		$dispFieldAlias = postvalue('dispFieldAlias');
		$dispField = postvalue('dispField');
		
		if ($dispFieldAlias)
		{
			$sqlHead.=", ".($dispField)." as ".AddFieldWrappers($dispFieldAlias)." ";
		}
		$strSQL = gSQLWhere_having($sqlHead, $gsqlFrom, $gsqlWhereExpr, $sqlGroupBy, $sqlHaving, $where, '');		
		
		LogInfo($strSQL);
		$rs=db_query($strSQL,$conn);
		$data=db_fetch_array($rs);
	}
	if(!$data)
	{
		$data=$avalues;
		$HaveData=false;
	}
	//check if correct values added
	$masterquery="mastertable=proforma%5Finvoices";
	$masterquery.="&masterkey1=".rawurlencode($data["id"]);
	$showDetailKeys["proforma_invoice_items"]=$masterquery;

	$keylink="";
	$keylink.="&key1=".htmlspecialchars(rawurlencode(@$data["id"]));
	
////////////////////////////////////////////
//	id - 
	$display = false;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"id", ""),"field=id".$keylink,"",MODE_LIST);
	$showValues["id"] = $value;
	$showFields[] = "id";
		$showRawValues["id"] = substr($data["id"],0,100);
	}	
//	account_id - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
				$value = DisplayLookupWizard("account_id",$data["account_id"],$data,$keylink,MODE_LIST);
	$showValues["account_id"] = $value;
	$showFields[] = "account_id";
		$showRawValues["account_id"] = substr($data["account_id"],0,100);
	}	
//	account_address - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"account_address", ""),"field=account%5Faddress".$keylink,"",MODE_LIST);
	$showValues["account_address"] = $value;
	$showFields[] = "account_address";
		$showRawValues["account_address"] = substr($data["account_address"],0,100);
	}	
//	buyer_id - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
				$value = DisplayLookupWizard("buyer_id",$data["buyer_id"],$data,$keylink,MODE_LIST);
	$showValues["buyer_id"] = $value;
	$showFields[] = "buyer_id";
		$showRawValues["buyer_id"] = substr($data["buyer_id"],0,100);
	}	
//	buyer_address - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"buyer_address", ""),"field=buyer%5Faddress".$keylink,"",MODE_LIST);
	$showValues["buyer_address"] = $value;
	$showFields[] = "buyer_address";
		$showRawValues["buyer_address"] = substr($data["buyer_address"],0,100);
	}	
//	consignee_id - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
				$value = DisplayLookupWizard("consignee_id",$data["consignee_id"],$data,$keylink,MODE_LIST);
	$showValues["consignee_id"] = $value;
	$showFields[] = "consignee_id";
		$showRawValues["consignee_id"] = substr($data["consignee_id"],0,100);
	}	
//	consignee_address - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"consignee_address", ""),"field=consignee%5Faddress".$keylink,"",MODE_LIST);
	$showValues["consignee_address"] = $value;
	$showFields[] = "consignee_address";
		$showRawValues["consignee_address"] = substr($data["consignee_address"],0,100);
	}	
//	notify_id - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
				$value = DisplayLookupWizard("notify_id",$data["notify_id"],$data,$keylink,MODE_LIST);
	$showValues["notify_id"] = $value;
	$showFields[] = "notify_id";
		$showRawValues["notify_id"] = substr($data["notify_id"],0,100);
	}	
//	notify_address - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"notify_address", ""),"field=notify%5Faddress".$keylink,"",MODE_LIST);
	$showValues["notify_address"] = $value;
	$showFields[] = "notify_address";
		$showRawValues["notify_address"] = substr($data["notify_address"],0,100);
	}	
//	proforma_invoice_no - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"proforma_invoice_no", ""),"field=proforma%5Finvoice%5Fno".$keylink,"",MODE_LIST);
	$showValues["proforma_invoice_no"] = $value;
	$showFields[] = "proforma_invoice_no";
		$showRawValues["proforma_invoice_no"] = substr($data["proforma_invoice_no"],0,100);
	}	
//	proforma_invoice_date - Short Date
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"proforma_invoice_date", "Short Date"),"field=proforma%5Finvoice%5Fdate".$keylink,"",MODE_LIST);
	$showValues["proforma_invoice_date"] = $value;
	$showFields[] = "proforma_invoice_date";
		$showRawValues["proforma_invoice_date"] = substr($data["proforma_invoice_date"],0,100);
	}	
//	customer_reference - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"customer_reference", ""),"field=customer%5Freference".$keylink,"",MODE_LIST);
	$showValues["customer_reference"] = $value;
	$showFields[] = "customer_reference";
		$showRawValues["customer_reference"] = substr($data["customer_reference"],0,100);
	}	
//	customer_reference_date - Short Date
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"customer_reference_date", "Short Date"),"field=customer%5Freference%5Fdate".$keylink,"",MODE_LIST);
	$showValues["customer_reference_date"] = $value;
	$showFields[] = "customer_reference_date";
		$showRawValues["customer_reference_date"] = substr($data["customer_reference_date"],0,100);
	}	
//	commercial_invoice_no - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"commercial_invoice_no", ""),"field=commercial%5Finvoice%5Fno".$keylink,"",MODE_LIST);
	$showValues["commercial_invoice_no"] = $value;
	$showFields[] = "commercial_invoice_no";
		$showRawValues["commercial_invoice_no"] = substr($data["commercial_invoice_no"],0,100);
	}	
//	commercial_invoice_date - Short Date
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"commercial_invoice_date", "Short Date"),"field=commercial%5Finvoice%5Fdate".$keylink,"",MODE_LIST);
	$showValues["commercial_invoice_date"] = $value;
	$showFields[] = "commercial_invoice_date";
		$showRawValues["commercial_invoice_date"] = substr($data["commercial_invoice_date"],0,100);
	}	
//	term_of_sale1 - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"term_of_sale1", ""),"field=term%5Fof%5Fsale1".$keylink,"",MODE_LIST);
	$showValues["term_of_sale1"] = $value;
	$showFields[] = "term_of_sale1";
		$showRawValues["term_of_sale1"] = substr($data["term_of_sale1"],0,100);
	}	
//	term_of_sale2 - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"term_of_sale2", ""),"field=term%5Fof%5Fsale2".$keylink,"",MODE_LIST);
	$showValues["term_of_sale2"] = $value;
	$showFields[] = "term_of_sale2";
		$showRawValues["term_of_sale2"] = substr($data["term_of_sale2"],0,100);
	}	
//	shipment_mode_id - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
				$value = DisplayLookupWizard("shipment_mode_id",$data["shipment_mode_id"],$data,$keylink,MODE_LIST);
	$showValues["shipment_mode_id"] = $value;
	$showFields[] = "shipment_mode_id";
		$showRawValues["shipment_mode_id"] = substr($data["shipment_mode_id"],0,100);
	}	
//	estimated_shipment_date - Short Date
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"estimated_shipment_date", "Short Date"),"field=estimated%5Fshipment%5Fdate".$keylink,"",MODE_LIST);
	$showValues["estimated_shipment_date"] = $value;
	$showFields[] = "estimated_shipment_date";
		$showRawValues["estimated_shipment_date"] = substr($data["estimated_shipment_date"],0,100);
	}	
//	estimated_shipment_date_custom - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"estimated_shipment_date_custom", ""),"field=estimated%5Fshipment%5Fdate%5Fcustom".$keylink,"",MODE_LIST);
	$showValues["estimated_shipment_date_custom"] = $value;
	$showFields[] = "estimated_shipment_date_custom";
		$showRawValues["estimated_shipment_date_custom"] = substr($data["estimated_shipment_date_custom"],0,100);
	}	
//	additional_charges_frieght_type - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"additional_charges_frieght_type", ""),"field=additional%5Fcharges%5Ffrieght%5Ftype".$keylink,"",MODE_LIST);
	$showValues["additional_charges_frieght_type"] = $value;
	$showFields[] = "additional_charges_frieght_type";
		$showRawValues["additional_charges_frieght_type"] = substr($data["additional_charges_frieght_type"],0,100);
	}	
//	additional_charges_legalization - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"additional_charges_legalization", ""),"field=additional%5Fcharges%5Flegalization".$keylink,"",MODE_LIST);
	$showValues["additional_charges_legalization"] = $value;
	$showFields[] = "additional_charges_legalization";
		$showRawValues["additional_charges_legalization"] = substr($data["additional_charges_legalization"],0,100);
	}	
//	additional_charges_inspection - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"additional_charges_inspection", ""),"field=additional%5Fcharges%5Finspection".$keylink,"",MODE_LIST);
	$showValues["additional_charges_inspection"] = $value;
	$showFields[] = "additional_charges_inspection";
		$showRawValues["additional_charges_inspection"] = substr($data["additional_charges_inspection"],0,100);
	}	
//	additional_charges_special_packing - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"additional_charges_special_packing", ""),"field=additional%5Fcharges%5Fspecial%5Fpacking".$keylink,"",MODE_LIST);
	$showValues["additional_charges_special_packing"] = $value;
	$showFields[] = "additional_charges_special_packing";
		$showRawValues["additional_charges_special_packing"] = substr($data["additional_charges_special_packing"],0,100);
	}	
//	others1_key - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"others1_key", ""),"field=others1%5Fkey".$keylink,"",MODE_LIST);
	$showValues["others1_key"] = $value;
	$showFields[] = "others1_key";
		$showRawValues["others1_key"] = substr($data["others1_key"],0,100);
	}	
//	others2_key - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"others2_key", ""),"field=others2%5Fkey".$keylink,"",MODE_LIST);
	$showValues["others2_key"] = $value;
	$showFields[] = "others2_key";
		$showRawValues["others2_key"] = substr($data["others2_key"],0,100);
	}	
//	others3_key - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"others3_key", ""),"field=others3%5Fkey".$keylink,"",MODE_LIST);
	$showValues["others3_key"] = $value;
	$showFields[] = "others3_key";
		$showRawValues["others3_key"] = substr($data["others3_key"],0,100);
	}	
//	others1_value - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"others1_value", ""),"field=others1%5Fvalue".$keylink,"",MODE_LIST);
	$showValues["others1_value"] = $value;
	$showFields[] = "others1_value";
		$showRawValues["others1_value"] = substr($data["others1_value"],0,100);
	}	
//	others2_value - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"others2_value", ""),"field=others2%5Fvalue".$keylink,"",MODE_LIST);
	$showValues["others2_value"] = $value;
	$showFields[] = "others2_value";
		$showRawValues["others2_value"] = substr($data["others2_value"],0,100);
	}	
//	others3_value - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"others3_value", ""),"field=others3%5Fvalue".$keylink,"",MODE_LIST);
	$showValues["others3_value"] = $value;
	$showFields[] = "others3_value";
		$showRawValues["others3_value"] = substr($data["others3_value"],0,100);
	}	
//	payment_term_id - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
				$value = DisplayLookupWizard("payment_term_id",$data["payment_term_id"],$data,$keylink,MODE_LIST);
	$showValues["payment_term_id"] = $value;
	$showFields[] = "payment_term_id";
		$showRawValues["payment_term_id"] = substr($data["payment_term_id"],0,100);
	}	
//	payment_term_other - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"payment_term_other", ""),"field=payment%5Fterm%5Fother".$keylink,"",MODE_LIST);
	$showValues["payment_term_other"] = $value;
	$showFields[] = "payment_term_other";
		$showRawValues["payment_term_other"] = substr($data["payment_term_other"],0,100);
	}	
//	bank_name_id - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
				$value = DisplayLookupWizard("bank_name_id",$data["bank_name_id"],$data,$keylink,MODE_LIST);
	$showValues["bank_name_id"] = $value;
	$showFields[] = "bank_name_id";
		$showRawValues["bank_name_id"] = substr($data["bank_name_id"],0,100);
	}	
//	bank_term1 - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"bank_term1", ""),"field=bank%5Fterm1".$keylink,"",MODE_LIST);
	$showValues["bank_term1"] = $value;
	$showFields[] = "bank_term1";
		$showRawValues["bank_term1"] = substr($data["bank_term1"],0,100);
	}	
//	bank_term2 - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"bank_term2", ""),"field=bank%5Fterm2".$keylink,"",MODE_LIST);
	$showValues["bank_term2"] = $value;
	$showFields[] = "bank_term2";
		$showRawValues["bank_term2"] = substr($data["bank_term2"],0,100);
	}	
//	currency_id - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
				$value = DisplayLookupWizard("currency_id",$data["currency_id"],$data,$keylink,MODE_LIST);
	$showValues["currency_id"] = $value;
	$showFields[] = "currency_id";
		$showRawValues["currency_id"] = substr($data["currency_id"],0,100);
	}	
//	terms_and_conditions - 
	$display = false;
	if($inlineadd==ADD_MASTER)
		$display = true;
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		$display = true;
	if($display)
	{	
		$value="";
			$value = ProcessLargeText(GetData($data,"terms_and_conditions", ""),"field=terms%5Fand%5Fconditions".$keylink,"",MODE_LIST);
	$showValues["terms_and_conditions"] = $value;
	$showFields[] = "terms_and_conditions";
		$showRawValues["terms_and_conditions"] = substr($data["terms_and_conditions"],0,100);
	}	
	
	// for custom expression for display field
	if ($dispFieldAlias)
	{
		$showValues[] = $data[$dispFieldAlias];	
		$showFields[] = $dispFieldAlias;
		$showRawValues[] = substr($data[$dispFieldAlias],0,100);
	}		
	
	if($inlineadd==ADD_INLINE || $inlineadd==ADD_POPUP)
	{	
		if($IsSaved && count($showValues))
		{		
			$returnJSON['success'] = true;	
			if($HaveData){
				$returnJSON['noKeys'] = false;
			}else{
				$returnJSON['noKeys'] = true;
			}
				
			$returnJSON['keys'] = $keys;
			$returnJSON['vals'] = $showValues;
			$returnJSON['fields'] = $showFields;
			$returnJSON['rawVals'] = $showRawValues;
			$returnJSON['detKeys'] = $showDetailKeys;
			$returnJSON['userMess'] = $usermessage;
		}
		else
		{
			$returnJSON['success'] = false;
			$returnJSON['message'] = $message;
		}
		echo "<textarea>".htmlspecialchars(my_json_encode($returnJSON))."</textarea>";
		exit();
	}	
} 

/////////////////////////////////////////////////////////////
if($inlineadd==ADD_MASTER)
{		
	$respJSON = array();
	if(($_POST["a"]=="added" && $IsSaved))
	{
		$respJSON['success'] = true;
		$respJSON['fields'] = $showFields;
		$respJSON['vals'] = $showValues;
		if($onFly){
			if($HaveData)
				$returnJSON['noKeys'] = false;
			else
				$returnJSON['noKeys'] = true;
			$respJSON['keys'] = $keys;
			$respJSON['rawVals'] = $showRawValues;
			$respJSON['detKeys'] = $showDetailKeys;
			$respJSON['userMess'] = $usermessage;
		}
		$respJSON['mKeys'] = array();	
		for($i=0;$i<count($dpParams['ids']);$i++)
		{
			$data=0;
			if(count($keys))
			{
				$where=KeyWhere($keys);
							$strSQL = gSQLWhere($where);
				LogInfo($strSQL);
				$rs=db_query($strSQL,$conn);
				$data=db_fetch_array($rs);
			}
			if(!$data)
				$data=$avalues;
			
			$mKeyId = 1;
			foreach($mKeys[$dpParams['strTableNames'][$i]] as $mk)	
			{
				if($data[$mk])
					$respJSON['mKeys'][$dpParams['strTableNames'][$i]]['masterkey'.$mKeyId++] = $data[$mk];
				else
					$respJSON['mKeys'][$dpParams['strTableNames'][$i]]['masterkey'.$mKeyId++] = '';
			}		
		}
		if((isset($_SESSION[$strTableName."_count_captcha"])) or ($_SESSION[$strTableName."_count_captcha"]>0) or ($_SESSION[$strTableName."_count_captcha"]<5))
			$respJSON['hideCaptha'] = true;
	}
	else{
			$respJSON['success'] = false;
			if(!$pageObject->isCaptchaOk)
				$respJSON['captha'] = false;
			else		
				$respJSON['error'] = $message;
			if($onFly)
				$respJSON['message'] = $message;				
		}
	echo "<textarea>".htmlspecialchars(my_json_encode($respJSON))."</textarea>";	
	exit();
}

/////////////////////////////////////////////////////////////
//	prepare Edit Controls
/////////////////////////////////////////////////////////////

//	validation stuff
$regex='';
$regexmessage='';
$regextype = '';
$control = array();

foreach($addFields as $fName)
{
	$gfName = GoodFieldName($fName);
	$controls = array('controls'=>array());
	if(!$detailKeys || !in_array($fName, $detailKeys) || $fName == postvalue("category"))
	{		
		$control[$gfName] = array();
		$control[$gfName]["func"]="xt_buildeditcontrol";
		$control[$gfName]["params"] = array();
		$control[$gfName]["params"]["id"]= $id;
		$control[$gfName]["params"]["field"]=$fName;
		$control[$gfName]["params"]["value"]=@$defvalues[$fName];
		if(UseRTE($fName))
			$_SESSION[$strTableName."_".$fName."_rte"]=@$defvalues[$fName];
		
		//	Begin Add validation
		$arrValidate = getValidation($fName,$strTableName);	
		$control[$gfName]["params"]["validate"] = $arrValidate;
		//	End Add validation	
	}
	$controls["controls"]['ctrlInd'] = 0;
	$controls["controls"]['id'] = $id;
	$controls["controls"]['fieldName'] = $fName;
	
	if(UseRTEFCK($fName) || UseRTEInnova($fName) || UseRTEBasic($fName))
	{
		if(!$detailKeys || !in_array($fName, $detailKeys))	
			$control[$gfName]["params"]["mode"]="add";
		$controls["controls"]['mode'] = "add";
	}
	else
	{
		if($inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
		{
			if(!$detailKeys || !in_array($fName, $detailKeys) || $fName == postvalue("category"))	
				$control[$gfName]["params"]["mode"]="inline_add";
			$controls["controls"]['mode'] = "inline_add";
		}
		else
		{
			if(!$detailKeys || !in_array($fName, $detailKeys) || $fName == postvalue("category"))	
				$control[$gfName]["params"]["mode"]="add";
			$controls["controls"]['mode'] = "add";
		}
	}
			
	if(!$detailKeys || !in_array($fName, $detailKeys))
		$xt->assignbyref($gfName."_editcontrol",$control[$gfName]);
	elseif($detailKeys && in_array($fName, $detailKeys))
		$controls["controls"]['value'] = @$defvalues[$fName];
		
	// category control field
	$strCategoryControl = $pageObject->hasDependField($fName);
	
	if($strCategoryControl!==false && in_array($strCategoryControl, $addFields))
		$vals = array($fName => @$defvalues[$fName],$strCategoryControl => @$defvalues[$strCategoryControl]);
	else
		$vals = array($fName => @$defvalues[$fName]);
	
	$preload = $pageObject->fillPreload($fName, $vals);
	if($preload!==false)
		$controls["controls"]['preloadData'] = $preload;
	
	$pageObject->fillControlsMap($controls);
	
	//fill field tool tips
	$pageObject->fillFieldToolTips($fName);
	
	// fill special settings for timepicker 	
	if(GetEditFormat($fName) == 'Time')	
		$pageObject->fillTimePickSettings($fName, @$defvalues[$fName]);
	
	if((($detailKeys && in_array($fName, $detailKeys)) || $fName == postvalue("category")) && array_key_exists($fName, $defvalues))
	{
		if((GetEditFormat($fName)==EDIT_FORMAT_LOOKUP_WIZARD || GetEditFormat($fName)==EDIT_FORMAT_RADIO) && GetpLookupType($fName) == LT_LOOKUPTABLE)
			$value=DisplayLookupWizard($fName,$defvalues[$fName],$defvalues,"",MODE_VIEW);
		elseif(NeedEncode($fName))
			$value = ProcessLargeText(GetData($defvalues,$fName, ViewFormat($fName)),"field=".rawurlencode(htmlspecialchars($fName)),"",MODE_VIEW);
		else
			$value = GetData($defvalues,$fName, ViewFormat($fName));
		
		$xt->assign($gfName."_editcontrol", $value);
	}
}
//fill tab groups name and sections name to controls
$pageObject->fillCntrlTabGroups();

//fill jsSettings and ControlsHTMLMap
$pageObject->fillSetCntrlMaps();

/////////////////////////////////////////////////////////////
if($pageObject->isShowDetailTables && ($inlineadd==ADD_SIMPLE || $inlineadd==ADD_POPUP))
{
	$options = array();
	//array of params for classes
	$options["mode"] = LIST_DETAILS;
	$options["pageType"] = PAGE_LIST;
	$options["masterPageType"] = PAGE_ADD;
	$options["mainMasterPageType"] = PAGE_ADD;
	$options['masterTable'] = $strTableName;
	$options['firstTime'] = 1;

	if(count($dpParams['ids']))
	{
		$xt->assign("detail_tables",true);
		include('classes/listpage.php');
		include('classes/listpage_embed.php');
		include('classes/listpage_dpinline.php');
		include("classes/searchclause.php");
	}
	
	$dControlsMap = array();
		
	$flyId = $ids+1;
	for($d=0;$d<count($dpParams['ids']);$d++)
	{
		$strTableName = $dpParams['strTableNames'][$d];
		include("include/".GetTableURL($strTableName)."_settings.php");
		$options['xt'] = new Xtempl();
		$options['id'] = $dpParams['ids'][$d];
		$options['flyId'] = $flyId++;
		$mkr=1;
		
		foreach($mKeys[$strTableName] as $mk)
		{
			if($defvalues[$mk])
				$options['masterKeysReq'][$mkr++] = $defvalues[$mk];
			else
				$options['masterKeysReq'][$mkr++] = '';
		}
		
		$listPageObject = ListPage::createListPage($strTableName,$options);
		// prepare code
		$listPageObject->prepareForBuildPage();
		$flyId = $listPageObject->recId+1;
		
		if($listPageObject->isDispGrid())
		{
			//add detail settings to master settings
			$listPageObject->fillSetCntrlMaps();
			$pageObject->jsSettings['tableSettings'][$strTableName]	= $listPageObject->jsSettings['tableSettings'][$strTableName];		
			$dControlsMap[$strTableName]['gridRows'] = $listPageObject->controlsHTMLMap[$strTableName][PAGE_LIST][$dpParams['ids'][$d]]['gridRows'];
			$dControlsMap[$strTableName]['video'] = $listPageObject->controlsHTMLMap[$strTableName][PAGE_LIST][$dpParams['ids'][$d]]['video'];
			$dControlsMap[$strTableName]['gMaps'] = $listPageObject->controlsHTMLMap[$strTableName][PAGE_LIST][$dpParams['ids'][$d]]['gMaps'];
			foreach($listPageObject->jsSettings['global']['shortTNames'] as $key=>$val)
			{
				if(!array_key_exists($key,$pageObject->jsSettings['global']['shortTNames']))
					$pageObject->jsSettings['global']['shortTNames'][$key] = $val;
			}	
			
			//Add detail's js files to master's files
			$pageObject->copyAllJSFiles($listPageObject->grabAllJSFiles());
			
			//Add detail's css files to master's files	
			$pageObject->copyAllCSSFiles($listPageObject->grabAllCSSFiles());
		}
		$xt->assign("displayDetailTable_".GoodFieldName($strTableName), array("func" => "showDetailTable","params" => array("dpObject" => $listPageObject, "dpParams" => $strTableName)));
	}
	$strTableName = "proforma_invoices";
	$pageObject->controlsHTMLMap[$strTableName][PAGE_ADD][$id]['dControlsMap'] = $dControlsMap;	
}
/////////////////////////////////////////////////////////////

if($inlineadd == ADD_SIMPLE)
{
	$pageObject->body['end'] .= '<script>';
	$pageObject->body['end'] .= "window.controlsMap = '".jsreplace(my_json_encode($pageObject->controlsHTMLMap))."';";
	$pageObject->body['end'] .= "window.settings = '".jsreplace(my_json_encode($pageObject->jsSettings))."';";
	$pageObject->body['end'] .= '</script>';
}
else{
		$returnJSON['controlsMap'] = $pageObject->controlsHTMLMap;
		//if($isNeedSettings)
		$returnJSON['settings'] = $pageObject->jsSettings;	
	}

$pageObject->addCommonJs();

$jscode = $pageObject->PrepareJS();
if($inlineadd==ADD_SIMPLE)
{
	$pageObject->body["end"] .= "<script>".$jscode."</script>";
	$xt->assign("body",$pageObject->body);
	$xt->assign("flybody",true);
}
elseif($inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_MASTER || $inlineadd==ADD_POPUP)
{ 
	$xt->assign("footer","");
	$xt->assign("flybody",$pageObject->body);
	$xt->assign("body",true);
}	

$xt->assign("style_block",true);
$pageObject->xt->assign("legend", true);

if($eventObj->exists("BeforeShowAdd"))
	$eventObj->BeforeShowAdd($xt,$templatefile);

if($inlineadd==ADD_ONTHEFLY || $inlineadd==ADD_POPUP)
{
	$xt->load_template($templatefile);
	$returnJSON['html'] = $xt->fetch_loaded('style_block').$xt->fetch_loaded('flybody');
	if($inlineadd==ADD_POPUP && $pageObject->isShowDetailTables)
		$returnJSON['html'].= $xt->fetch_loaded('detail_tables');
	$returnJSON['idStartFrom'] = $id+1;	
	echo (my_json_encode($returnJSON)); 
}
elseif ($inlineadd == ADD_INLINE)
{
	$xt->load_template($templatefile);
	$returnJSON["html"] = array();
	foreach($addFields as $fName)
	{
		$returnJSON["html"][$fName] = $xt->fetchVar(GoodFieldName($fName)."_editcontrol");	
	}	
	echo (my_json_encode($returnJSON)); 
}
else
	$xt->display($templatefile);

?>
